package de.hsos.bose.skillshare.model.core.table;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import de.hsos.bose.skillshare.model.core.DataAccess;
import de.hsos.bose.skillshare.model.objects.Mitarbeiter;

public class AssignmentTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private static final String resFirstName = "Prename";
	private static final String resName = "Name";
	private static final String resOccupation = "Job";
	
	public static final int FIRSNAME_INDEX = 0;
	public static final int NAME_INDEX = 1;
	public static final int OCCUPATION_INDEX = 2;
	
	protected String[] columnNames = { resFirstName, resName, resOccupation};
	protected Vector<Mitarbeiter> dataVector;
	
	public AssignmentTableModel() {
		this.dataVector = new Vector<Mitarbeiter>();
	}
	
	public void loadEmpoyees(int adminId) {
		while(dataVector.size() > 0){
			dataVector.remove(0);
			fireTableRowsDeleted(0, 0);
		}
		DataAccess da = new DataAccess();
		List<Mitarbeiter> mitaList = da.getAssignMitarbeiter(adminId);

		for (Mitarbeiter mitarbeiter : mitaList) {
			dataVector.add(mitarbeiter);
			fireTableRowsInserted(dataVector.size(), dataVector.size());
		}
	}
	
	public Mitarbeiter getMitarbeiter(int index) {
		return dataVector.get(index);
	}

	@Override
	public String getColumnName(int column) {
		return columnNames[column];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
	}
	
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
			case FIRSNAME_INDEX:
			case NAME_INDEX:
			case OCCUPATION_INDEX:
				return String.class;
			default:
				return Object.class;
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Mitarbeiter record = (Mitarbeiter) dataVector.get(rowIndex);
		switch (columnIndex) {
		case FIRSNAME_INDEX:
			return record.getVorname();
		case NAME_INDEX:
			return record.getName();
		case OCCUPATION_INDEX:
			return record.getBeruf();
		default:
			return new Object();
		}
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		Mitarbeiter record = (Mitarbeiter) dataVector.get(rowIndex);
		switch (columnIndex) {
		case FIRSNAME_INDEX:
			record.setVorname((String) aValue);
			break;
		case NAME_INDEX:
			record.setName((String) aValue);
			break;
		case OCCUPATION_INDEX:
			record.setBeruf((String) aValue);
			break;
		default:
			System.out.println("invalid index");
		}
		fireTableCellUpdated(rowIndex, columnIndex);
	}

	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return dataVector.size();
	}

}
